﻿using System.Windows.Forms;
using GeoSketch2.Core;

namespace GeoSketch2.Archaeo
{
    /// <summary>
    /// The cloning tool
    /// Select objects to clone, then select the cloning tool, click to origin point, then the destination point
    /// </summary>
    public class TCLone:BaseArchaeoTool
    {
        #region Fields

        Point2D64D BasePoint;

        #endregion

        #region Constructor
        /// <summary> 
        /// The tool to clone objects
        /// </summary>
        /// <param name="Viewport">The viewport to draw to</param>
        public TCLone(Viewport Viewport)
            : base(Viewport,"Clone")
        {
        } 
        #endregion

        #region Inputs
        /// <summary>
        /// The method called when the mouse button is down
        /// </summary>
        public override void MouseDown(MouseEventArgs MouseArgs)
        {
            ExternalPointSent(Viewport.Object2ImageTransformation.InverseTransform(MouseArgs.Location));
        }

        /// <summary>
        /// The method called when a text is sent to the tool
        /// </summary>
        public override void TextEntry(string Text)
        {
            ExternalPointSent(GetCoordinatesFromString(Text));
        }

        /// <summary>
        /// The method called when a point in object coordinates is sent to the tool
        /// </summary>
        public override void ExternalPointSent(Point2D64D Point)
        {
            if (BasePoint == null)
            {
                BasePoint = Point.DeepClone();
                SendMessage("Select insertion point");
            }
            else
            {
                Point2D64D Vector = Point - BasePoint;

                foreach (BaseArchaeoDrawableObject obj in Viewport.SelectedObjects)
                {
                    BaseArchaeoDrawableObject clone = obj.Clone<BaseArchaeoDrawableObject>();
                    clone.Translate(Vector);
                    clone.ExcavationArea = Viewport.CurrentExcavationArea;
                    clone.Layer = Viewport.CurrentLayer;
                    Viewport.GeoObjects.Add(clone);
                    obj.IsSelected = false;
                }
                Viewport.SelectedObjects.Clear();
                RefreshViewport();
            }
        }
        #endregion

        #region Methods
        /// <summary>
        /// The method to be used to send the first message to the user, just after beeing selected
        /// </summary>
        public override void SendFirstMessage()
        {
            SendMessage("Select source point");
        } 
        #endregion
    }
}